package com.example.demoss.service.impl;

import com.example.demoss.entity.*;
import com.example.demoss.mapper.Dec001Mapper;
import com.example.demoss.service.IndexService;
import com.example.demoss.service.TrueService;
import com.example.demoss.util.ProviceUtil;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 黄靖
 * @author 2022-12-12 17:46
 * description:
 **/
@Slf4j
@Service
public class TrueServiceImpl implements TrueService {

    @Resource
    private Dec001Mapper mapper;

    @Resource
    private IndexService indexService;


    @Override
    public ResultCountry listByCountry(ArrayList<ResultVo> resultVos) {

        //处理 cityVo
        final  List<CityVo> cityVos = new ArrayList<>();
        final List<RegionVo> regionVos = new ArrayList<>();
        final ResultCountry resultCountry = new ResultCountry();



        for (int i=0;i<resultVos.size();i++
             ) {
            ResultVo result = resultVos.get(i);

            //封装cityvo
             List<CityVo> cityVos1 = result.getCityVos();

             CityVo cityVo = getCityVo(cityVos1);
             cityVo.setDay(i+1);
        log.info("city 处理后的 cirty===>",cityVo);
             cityVos.add(cityVo);

             //封装regionvo
            List<RegionVo> regionVos1 = result.getRegionVos();
            int finalI = i;
            regionVos1.forEach(regionVo -> regionVo.setDay(finalI +1));
            log.info("region 传入的 对象==》",regionVos1);
            regionVos.addAll(regionVos1);

        }


resultCountry.setCityVoList(cityVos);
resultCountry.setAllRegionList(regionVos);

        return resultCountry;
    }

    @Override
    public ResultVo listBySeason(List<Integer> list,String season) {
          List<CityVo> cityVosAll= new ArrayList<>();
        List<RegionVo> regionVosAll=new ArrayList<>();
        List<CityVo> trueCityVos =new ArrayList<>();

        //组装region
        list.forEach(day->{

            final ResultVo allDataByOneDay = getAllDataByOneDay(day);
            final List<CityVo> cityVos = allDataByOneDay.getCityVos();
            final List<RegionVo> regionVos = allDataByOneDay.getRegionVos();

            cityVosAll.addAll(cityVos);
            regionVosAll.addAll(regionVos);
        });






        //组装 city
        for (ProviceUtil provice:ProviceUtil.provinceList
             ) {
            final List<CityVo> collect = cityVosAll.stream().filter(cityVo -> cityVo.getCity().equals(provice.getProvince().trim())).collect(Collectors.toList());
            final CityVo cityVo = getCityVo(collect);
            trueCityVos.add(cityVo);
        }





        final ResultVo resultVo = new ResultVo();
        resultVo.setParameter(season);
        //开始组装region
        resultVo.setRegionVos(  getRegionVos(regionVosAll));
        resultVo.setCityVos(trueCityVos);

       return resultVo;
    }

    @Override
    public ResultVo getAllDataByOneDay(int day) {

        String tableName = "mar_0";
        if (day < 10) {
            tableName = tableName + "0" + String.valueOf(day);
        }else {
        tableName = tableName + String.valueOf(day);
        }

        //拿到一天的各个省市的 数据
        List<Dec> decList = mapper.selectAllAverage(tableName);
        decList = decList.stream().filter(dec -> !"".equals(dec.getCity())).collect(Collectors.toList());

        //开始组装 region
        decList.stream().forEach(dec ->dec.setRegion( indexService.regionOne(dec)));
        final List<Dec> decRegionList = decList.stream().filter(dec -> StringUtils.isNotEmpty(dec.getRegion())).collect(Collectors.toList());

        List<RegionVo> regionVos = new ArrayList<>();

            for (Dec dec:decRegionList
                 ) {
                RegionVo regionVo = new RegionVo();
                BeanUtils.copyProperties(dec,regionVo);
                regionVos.add(regionVo);

            }


        //组装 每天 每省的 平均值集合
        final List<CityVo> cityVos = new ArrayList<>();
        decList.forEach(dec -> {
            final CityVo cityVo = new CityVo();
            BeanUtils.copyProperties(dec,cityVo);
            cityVos.add(cityVo);
        });


        //开始组装 各个地区的平均值

         regionVos = getRegionVos(regionVos);
        final ResultVo resultVo = new ResultVo();
      resultVo.setParameter(String.valueOf(day));
        resultVo.setRegionVos(regionVos);
        resultVo.setCityVos(cityVos);


        resultVo.setCitySize(cityVos.size());

        return resultVo;
    }

    private List<RegionVo> getRegionVos(List<RegionVo> decList) {

         List<RegionVo> regionVos = new ArrayList<>();
        final List<RegionVo> huaDongList = decList.stream().filter(dec -> dec.getRegion().equals("华东")).collect(Collectors.toList());
        final List<RegionVo> huaBeiList = decList.stream().filter(dec -> dec.getRegion().equals("华北")).collect(Collectors.toList());
        final List<RegionVo> huaNanList = decList.stream().filter(dec -> dec.getRegion().equals("华南")).collect(Collectors.toList());
        final List<RegionVo> huaZhongList = decList.stream().filter(dec -> dec.getRegion().equals("华中")).collect(Collectors.toList());


        regionVos.add(getRegionVo(huaDongList));
        regionVos.add(getRegionVo(huaBeiList));
        regionVos.add(getRegionVo( huaNanList));
        regionVos.add(getRegionVo(huaZhongList));
return regionVos;
    }

    private RegionVo getRegionVo(List<RegionVo> list){



    String region = list.get(0).getRegion();
    Double size =Double.valueOf( list.size());





    String pm25Average =Double.toString( list.stream().map(RegionVo::getPm25).mapToDouble(x->Double.valueOf(x)).sum()/size);

    pm25Average=pm25Average.substring(0,pm25Average.indexOf(".")+2);
   String pm10Average =Double.toString( list.stream().map(RegionVo::getPm10).mapToDouble(x->Double.valueOf(x)).sum()/size);
    pm10Average=pm10Average.substring(0,pm10Average.indexOf(".")+2);


    String tempAverage =Double.toString( list.stream().map(RegionVo::getTemp).mapToDouble(x->Double.valueOf(x)).sum()/size);
    tempAverage=tempAverage.substring(0,tempAverage.indexOf(".")+2);


     String so2Average = Double.toString(list.stream().map(RegionVo::getSo2).mapToDouble(x->Double.valueOf(x)).sum()/size);
    so2Average=so2Average.substring(0,so2Average.indexOf(".",1)+2);

     String no2Average =Double.toString( list.stream().map(RegionVo::getNo2).mapToDouble(x->Double.valueOf(x)).sum()/size);
    no2Average=no2Average.substring(0,no2Average.indexOf(".")+2);

     String O3Average = Double.toString(list.stream().map(RegionVo::getO3).mapToDouble(x->Double.valueOf(x)).sum()/size);

    O3Average=O3Average.substring(0,O3Average.indexOf(".")+2);
    String coAverage = Double.toString(list.stream().map(RegionVo::getCo).mapToDouble(x->Double.valueOf(x)).sum()/size);
    coAverage=coAverage.substring(0,coAverage.indexOf(".")+2);

   String aqiAverage = Double.toString(list.stream().map(RegionVo::getAqi).mapToDouble(x->Double.valueOf(x)).sum()/size);
    aqiAverage =aqiAverage .substring(0,aqiAverage .indexOf(".")+2);

    String uAverage = Double.toString(list.stream().map(RegionVo::getU).mapToDouble(x->Double.valueOf(x)).sum()/size);
    uAverage=uAverage.substring(0,uAverage.indexOf(".")+2);

    String vAverage = Double.toString(list.stream().map(RegionVo::getV).mapToDouble(x->Double.valueOf(x)).sum()/size);
    vAverage=vAverage.substring(0,vAverage.indexOf(".")+2);




    final RegionVo regionVo = new RegionVo(pm25Average,pm10Average,so2Average,no2Average,coAverage,O3Average,aqiAverage);

    regionVo.setTemp(tempAverage);
    regionVo.setU(uAverage);
    regionVo.setV(vAverage);
regionVo.setRegion(region);



    return regionVo;
}



    private CityVo getCityVo(List<CityVo> list){

        Double size =Double.valueOf(list.size());

        String pm25Average =Double.toString( list.stream().map(CityVo::getPm25).mapToDouble(x->Double.valueOf(x)).sum()/size);
        pm25Average=pm25Average.substring(0,pm25Average.indexOf(".")+2);
        String pm10Average =Double.toString( list.stream().map(CityVo::getPm10).mapToDouble(x->Double.valueOf(x)).sum()/size);
        pm10Average=pm10Average.substring(0,pm10Average.indexOf(".")+2);

        String so2Average = Double.toString(list.stream().map(CityVo::getSo2).mapToDouble(x->Double.valueOf(x)).sum()/size);
        so2Average=so2Average.substring(0,so2Average.indexOf(".",1)+2);

        String tempAverage =Double.toString( list.stream().map(CityVo::getTemp).mapToDouble(x->Double.valueOf(x)).sum()/size);
        tempAverage=tempAverage.substring(0,tempAverage.indexOf(".")+2);

        String no2Average =Double.toString( list.stream().map(CityVo::getNo2).mapToDouble(x->Double.valueOf(x)).sum()/size);
        no2Average=no2Average.substring(0,no2Average.indexOf(".")+2);

        String O3Average = Double.toString(list.stream().map(CityVo::getO3).mapToDouble(x->Double.valueOf(x)).sum()/size);
        O3Average=O3Average.substring(0,O3Average.indexOf(".")+2);

        String coAverage = Double.toString(list.stream().map(CityVo::getCo).mapToDouble(x->Double.valueOf(x)).sum()/size);
        coAverage=coAverage.substring(0,coAverage.indexOf(".")+2);


        String uAverage = Double.toString(list.stream().map(CityVo::getU).mapToDouble(x->Double.valueOf(x)).sum()/size);
        uAverage=uAverage.substring(0,uAverage.indexOf(".")+2);

        String vAverage = Double.toString(list.stream().map(CityVo::getV).mapToDouble(x->Double.valueOf(x)).sum()/size);
        vAverage=vAverage.substring(0,vAverage.indexOf(".")+2);



        String aqiAverage = Double.toString(list.stream().map(CityVo::getAqi).mapToDouble(x->Double.valueOf(x)).sum()/size);
        aqiAverage =aqiAverage .substring(0,aqiAverage .indexOf(".")+2);
        final   CityVo     cityVo = new     CityVo(pm25Average,pm10Average,so2Average,no2Average,coAverage,O3Average,aqiAverage,list.get(0).getCity());

cityVo.setTemp(tempAverage);
cityVo.setU(uAverage);
cityVo.setV(vAverage);
        return   cityVo;
    }





}



